package com.naizhuai.ui.msgmanager.multtype;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.naizhuai.R;
import com.naizhuai.ui.components.ExpandableTextView;
import com.naizhuai.ui.entity.User.AppMessageList;

import butterknife.BindView;
import butterknife.ButterKnife;
import me.drakeet.multitype.ItemViewBinder;

/**
 * @author：lichenxi
 * @date 2018/6/23 16
 * email：525603977@qq.com
 * Fighting
 */
public class StackMailViewBinder extends ItemViewBinder<AppMessageList, StackMailViewBinder.ViewHolder> {

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_stack_mail, parent, false);
        return new ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull AppMessageList stackMail) {

        holder.expandTextView.setText(stackMail.content);
        Context context = holder.itemView.getContext();
//        if ("1".equals(stackMail.isRead)) {
//            holder.redMsg.setVisibility(View.VISIBLE);
//        } else {
//            holder.redMsg.setVisibility(View.GONE);
//        }
        holder.expandTextView.setOnExpandStateChangeListener(new ExpandableTextView.OnExpandStateChangeListener() {
            @Override
            public void onExpandStateChanged(TextView textView, boolean isExpanded) {
                if (isExpanded){
                    holder.tagState.setText(context.getString(R.string.pack_up));
                    rotateArrow(holder.arrow, false);
                }else{
                    holder.tagState.setText(context.getString(R.string.more));
                    rotateArrow(holder.arrow, true);
                }
            }
        });
//        holder.itemView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                //文字改变 图标改变  文字变得更多 现在文字三行喽
////                stackMail.toggleStated();
////                if (stackMail.isOpen()) {
//                    holder.tagState.setText(context.getString(R.string.pack_up));
//                    rotateArrow(holder.arrow, false);
////                    holder.title.setMaxLines(Integer.MAX_VALUE);
////                } else {
//
////                    holder.title.setMaxLines(3);
////                }
//            }
//        });
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.expand_text_view)
        ExpandableTextView expandTextView;
        @BindView(R.id.tag_state)
        TextView tagState;
        @BindView(R.id.arrow)
        ImageView arrow;
//        @BindView(R.id.red_msg)
//        View redMsg;
        ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    private void rotateArrow(ImageView arrow, boolean flag) {
        float pivotX = arrow.getWidth() / 2f;
        float pivotY = arrow.getHeight() / 2f;
        float fromDegrees = 0f;
        float toDegrees = 0f;
        if (flag) {
            fromDegrees = 180f;
            toDegrees = 0f;
        } else {
            fromDegrees = 0f;
            toDegrees = 180f;
        }
        RotateAnimation animation = new RotateAnimation(fromDegrees, toDegrees,
                pivotX, pivotY);
        animation.setDuration(100);

        animation.setFillAfter(true);
        arrow.startAnimation(animation);
    }
}
